<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\UsersRoles;

class UsersrolesController extends Base
{

	function index()
	{
		$weid = weid();
		$keyword = trim(input('post.keyword', '', 'serach_in'));
		$status = trim(input('post.status', '', 'serach_in'));

		$where = [];
		$where['weid'] = $weid;
		$where['sid'] = (int) $this->sid;
		$where['ocid'] = (int) $this->ocid;
		if ($status !== '') {
			$where['status'] = $status;
		}

		$field = 'id,pid,title,status,description';
		$query = UsersRoles::where($where);

		if (!empty($keyword)) {
			$query->where('title', 'like', '%' . $keyword . '%');
		}

		//UsersRoles::getsonid($this->userInfo['role_id']);

		$query->field($field)->order('id desc');

		$datalist = $query->select()->toArray();

		if (empty($datalist) && empty($this->sid) && empty($this->ocid)) {
			UsersRoles::datainitial();
			$datalist = $query->select()->toArray();
		}
		$pid = $this->userInfo['role_id'];
		if (!empty($pid)) {
			$tmppid = UsersRoles::getPid($pid);
			if ($tmppid == 0) {
				$pid = 0;
			}
		}
		$data['data'] = _generateListTree($datalist, $pid, ['id', 'pid']);
		return $this->json($data);
	}

	function listUpdate()
	{
		$data = only('id,status');
		if (!$data['id']) throw new ValidateException('参数错误');
		UsersRoles::update($data);
		return $this->json(['msg' => '操作成功']);
	}

	public function update()
	{

		$id = $this->request->post('id');
		$data = only('id,pid,title,datarules,status,description');

		if (empty($id)) {
			$data['weid'] = weid();
			$data['sid'] = (int) $this->sid;
			$data['ocid'] = (int) $this->ocid;
			$res = UsersRoles::create($data);
			return $this->json(['msg' => '添加成功', 'data' => $res->id]);
		} else {
			UsersRoles::update($data);
			return $this->json(['msg' => '修改成功']);
		}
	}

	public function permissions()
	{

		$id = $this->request->post('id');
		if (!empty($id)) {
			$data = only('id,is_allrole,is_console,is_admin,is_store,access');

			if (!in_array('Home', $data['access'])) {
				array_push($data['access'], 'Home');
			}

			$data['access'] = implode(',', $data['access']);

			UsersRoles::update($data);
			return $this->json(['msg' => '修改成功']);
		}
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) $this->error('参数错误');
		$res = UsersRoles::find($id);
		if (!empty($res)) {
			$res = $res->toArray();
		}
		$res['scope'] = explode(',', $res['scope']);

		$res['access'] = explode(',', $res['access']);

		if ($res['pid'] == 0) {
			$res['pidarray'] = $res;
		} else {
			$pidarray = UsersRoles::find($res['pid']);
			if (!empty($pidarray)) {
				$res['pidarray'] = $pidarray->toArray();
			}
		}

		return $this->json(['data' => $res]);
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		if (in_array(1, explode(',', $idx))) {
			$this->error('超级管理员分组禁止删除');
		}
		UsersRoles::destroy(['id' => explode(',', $idx)], true);
		return $this->json(['msg' => '操作成功']);
	}
	function getField()
	{
		$data['pids'] = _generateSelectTree(UsersRoles::getpcarray(['ocid' => $this->ocid, 'sid' => $this->sid]));

		return $this->json(['data' => $data]);
	}
}
